package com.gpj.netty.application.client;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.UUID;

/**
 * @className NetWorkClient
 * @description BIO客户端代码
 * @author GPJ
 * @date 2021/11/30 17:02
 * @version 1.0
 **/
@Slf4j
public class NetWorkMultiClient {
	public static void main(String[] args) throws IOException, InterruptedException {
		Socket socket = new Socket("127.0.0.1", 9000);
		log.debug("连接成功");
		boolean end = false;
		while (!end) {
			OutputStream out = socket.getOutputStream();
			Scanner scanner = new Scanner(System.in);

			if(!scanner.hasNextLine()) Thread.sleep(500);

			String msg = scanner.nextLine();
			if("end".equals(msg)) end = true;
			out.write(msg.getBytes());
			out.flush();

			byte[] bytes = new byte[1024];
			int num = socket.getInputStream().read(bytes);
			log.info("接收到服务端的数据：{}", new String(bytes, 0, num));
		}

		socket.close();
	}
}
